package com.zink.ezequiel.codes.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.TextView;
import com.zink.ezequiel.codes.R;
import com.zink.ezequiel.codes.data.model.Entry;

import java.util.ArrayList;
 
 
public class TagsAdapter extends BaseExpandableListAdapter {
 
	private int groupLayout;
	private int childLayout;
	private String response;
	private  Context context;
	private ArrayList<String> groups;
	private ArrayList<ArrayList<Entry>> childrens;
	
    //Initialize adapter
    public TagsAdapter(Context context, int groupLayout, int childLayout) {
        super();
        this.context = context;
        this.groups = new ArrayList<String>();
        this.childrens = new ArrayList<ArrayList<Entry>>();
        this.groupLayout = groupLayout;
        this.childLayout = childLayout;
    }
   
    public void setData(ArrayList<String> groups, ArrayList<ArrayList<Entry>> childrens) {
        this.groups = groups;
        this.childrens = childrens;    	
    }
    
	public Object getChild(int groupPos, int childrenPos) {
		return this.childrens.get(groupPos).get(childrenPos);
	}

	public long getChildId(int groupPos, int childPos) {
		return this.childrens.get(groupPos).get(childPos).getID();
	}

	public View getChildView(int groupPosition, int childPosition,
			boolean isLastChild, View convertView, ViewGroup parent) {
		   if (convertView == null) {
			   LayoutInflater inflater =  (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			   convertView = inflater.inflate(R.layout.tags_list_child, null);
		   }
		   Entry entry = (Entry) getChild(groupPosition, childPosition);
		   TextView entryName = (TextView) convertView.findViewById(R.id.entryName);
		   entryName.setText(entry.getEntryName());
		   
		   TextView userName = (TextView) convertView.findViewById(R.id.entryUserName);
		   userName.setText(entry.getUserName());
			    
		   return convertView;
	}


	public int getChildrenCount(int groupPos) {
		return this.childrens.get(groupPos).size();
	}


	public Object getGroup(int groupPos) {
		return this.groups.get(groupPos);
	}


	public int getGroupCount() {
		return this.groups.size();
	}


	public long getGroupId(int groupPosition) {
		return 0;
	}


	public View getGroupView(int groupPosition, boolean isExpanded,
			View convertView, ViewGroup parent) {
		if (convertView == null) {
			LayoutInflater inflater =  (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			convertView = inflater.inflate(R.layout.tags_list_group, null);
		}
			    
		TextView tagName = (TextView) convertView.findViewById(R.id.tagName);
		tagName.setText(groups.get(groupPosition));
		
		//TextView counter = (TextView) convertView.findViewById(R.id.counter);
		//counter.setText(String.valueOf(childrens.get(groupPosition).size()));
		return convertView;
	}

	public boolean hasStableIds() {
		return false;
	}

	public boolean isChildSelectable(int groupPosition, int childPosition) {
		return false;
	}
 
}

